package com.jhg.drgclinicalpathway.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jhg.drgclinicalpathway.domain.PathTpl;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface PathTplDao extends BaseMapper<PathTpl> {

    @Select({
            "select a.疾病编码,b.名称 from JC_疾病对应路径 a left join JC_疾病编码目录 b on a.疾病编码=b.编码 where 路径ID in(",
                    "select 路径ID from JC_疾病对应路径 where 疾病编码=#{code}",
                    ")"
    })
    public List<Map<String,Object>> selectByDisCode(String code);


    @Select({
            "select * from V病组标杆值 where 分组编码=#{code}"
    })
    public List<Map<String,Object>> 病组标植值(String code);


    @Select({
            "select top 1 * from LJ_临床路径目录 where ID in(select 路径ID from JC_疾病对应路径 where 疾病编码=#{c})"
    })
    public Map<String,Object> 临床路径目录(@Param("c") String code);



    @Select({
            "<script>",
            "select * from V病组标杆值 where 分组编码 in",
            "<foreach collection='codes' open='(' close=')' item='id' index='i' separator=','>",
			"#{id}",
			"</foreach>",
            "</script>"
    })
    public List<Map<String,Object>> 病组标植值ByCode(@Param("codes") List<String> codes);

    @Select("SELECT distinct 项目类型,项目名称,case when pg.id is null then '' else '已执行' end 状态 FROM dbo.V路径特征标志 bz\n" +
            " left JOIN dbo.LJ_病人路径匹配 pp ON pp.路径id = BZ.路径ID  and pp.是否主诊断 = 1  \n" +
            " left JOIN DBO.LJ_病人路径评估 PG ON bz.路径id = pg.路径ID AND pg.项目ID = BZ.项目ID  AND pp.病人标识 = pg.病人标识 \n" +
            "where bz.特征标志=2 and bz.项目类型 in ('药品','治疗','检验','检查') and PP.病人标识 =#{brid} order by 项目类型 ")
    public List<Map<String,Object>> pathDetailByBrid1(@Param("brid")  String brid);
    /*@Select("SELECT distinct zd.jbbm+'-'+zd.zdms +'('+CASE CCSTATE WHEN 1 THEN 'MCC' WHEN 2 THEN 'CC' WHEN 3 THEN '排除' ELSE NULL END+')' 诊断,\n" +
            "项目类型,项目名称,case when pg.id is null then '' else '已执行' end 状态 FROM dbo.V路径特征标志 bz\n" +
            " left JOIN dbo.LJ_病人路径匹配 pp ON pp.路径id = BZ.路径ID  and pp.是否主诊断 != 1  \n" +
            " left JOIN DBO.LJ_病人路径评估 PG ON bz.路径id = pg.路径ID AND pg.项目ID = BZ.项目ID  AND pp.病人标识 = pg.病人标识 \n" +
            " left JOIN TMP_诊断信息 zd on jzh= PP.病人标识 and jlly+zdlxid+zdcx=pp.疾病私码\n" +
            "where bz.特征标志=2 and bz.项目类型 in ('药品','治疗','检验','检查') and zd.CCSTATE in (1,2) and PP.病人标识 = #{brid} \n" +
            "order by 诊断,项目类型")*/
    @Select(
            "SELECT distinct tz.特征标志,jb.jbbm+'-'+jb.zdms +CASE jb.CCSTATE WHEN 1 THEN '('+'MCC'+')' WHEN 2 THEN '('+'CC'+')' WHEN 3 THEN '('+'排除'+')' ELSE '' END 诊断,\n"+
                    "tz.项目类型,tz.项目名称,'' as 状态\n"+
                    "FROM V路径特征标志 tz,TMP_诊断信息 jb,\n"+
                    "(  SELECT pp.路径id,zd.id,count(1) mba_count ,"+"sum(case when FY.项目ID is not null then 1 else 0 end) all_count\n"+
                    "FROM dbo.LJ_病人路径匹配 PP "+" join dbo.LJ_病人评估预警 yj ON pp.病人标识 = yj.病人标识 AND yj.预警ID = 15\n"
                    +"CROSS APPLY OPENJSON(CASE WHEN yj.信息参数 = '' THEN NULL ELSE yj.信息参数 END ) WITH (sm NVARCHAR(64)) sm \n"
                   +"left JOIN DBO.V路径特征标志 BZ ON PP.路径id = BZ.路径ID\n "
            +"\nAND  pp.疾病私码 = sm.sm"
            +"\nleft JOIN (select f.jzh,b.项目ID from TMP_费用信息 f,LJ_临床路径项目明细 b where f.XMBM = b.项目编码) FY  "
            +"\non PP.病人标识=FY.JZH AND FY.项目ID = BZ.项目ID"
            +"\nleft JOIN TMP_诊断信息 zd on zd.jzh= PP.病人标识 and zd.jlly+zd.zdlxid+zd.zdcx=pp.疾病私码"
            +"\nWHERE PP.是否主诊断=0 and bz.特征标志=2 and BZ.项目类型 in ('药品','治疗','检验','检查') "
            +"\nand PP.病人标识 = #{brid}"
            +"\ngroup by pp.路径id,zd.id  ) lj "
            +"\nwhere tz.[路径ID]=lj.[路径id] and lj.id=jb.id and tz.[特征标志]=2 and mba_count>0 and all_count=0 "
            )
    public List<Map<String,Object>> pathDetailByBrid2(@Param("brid")  String brid);
    @Select("select  信息参数 as 诊断名称  from LJ_病人评估预警 where 预警id=13 and 病人标识 = #{brid}")
    public List<Map<String,Object>> pathDetailByBrid3(@Param("brid")  String brid);

    @Select("select b.ssbm+'-'+b.ssmc as 主要诊断相关手术参考 from TMP_诊断信息 a \n" +
            "left join icd10_icd9 b on a.jbbm=b.jbbm \n" +
            "where a.jzh=#{brid} and a.zdlxid=3 and a.jlly=3 and a.zdcx=1 ")
    public List<Map<String,Object>> pathDetailByBrid4(@Param("brid")  String brid);

    @Select("SELECT FORMATMESSAGE(b.提示信息,dbo.SplitString(a.信息参数,'|',1),dbo.SplitString(a.信息参数,'|',2),dbo.SplitString(a.信息参数,'|',3)) 提示\n" +
            "FROM [DBO].[LJ_病人清单校验] (READPAST) A  JOIN DBO.JC_清单规则目录 (READPAST) B ON A.清单ID = B.ID\n" +
            "WHERE a.病人标识 = #{brid} ")
    public List<Map<String,Object>> pathDetailByBrid5(@Param("brid")  String brid);
}
